<template>
  <div class="app-container">
    <upload-excel-component :on-success="handleSuccess" />
    <!-- <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </el-table> -->
  </div>
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel'
import { uploadEmployeeExcel } from '@/api/employees'
import { excelToDate } from '@/utils/index'
export default {
  name: 'Import',
  components: {
    UploadExcelComponent
  },
  data() {
    return {
    }
  },
  methods: {
    async handleSuccess({ header, results }) {
      // console.log(header, results)
      // this.tableData = results
      // this.tableHeader = header
      const data = this.resolveExcelData(results)
      // console.log(data)
      // 发送请求批量导入
      await uploadEmployeeExcel(data)
      this.$message.success('批量导入成功')
      this.$router.back()
    },
    // 处理数据
    resolveExcelData(data) {
      const obj = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '聘用形式': 'formOfEmployment',
        '部门': 'departmentName'
      }
      const arr = []
      data.forEach(item => {
        const obj1 = {}
        for (const key in item) {
          if (key === '入职日期' || key === '转正日期') {
            // 把excel时间转换为js格式时间
            obj1[obj[key]] = excelToDate(item[key])
          } else {
            obj1[obj[key]] = item[key]
          }
        }
        arr.push(obj1)
      })
      return arr
    }
  }
}
</script>

<style>

</style>
